<?
class FileManagerDisplayer extends Core
{
    public $uploadPrefix = "";
    public $fileManager;
    public $maxAttachments = 10;
    
    public function __construct($fileManager = NULL)
    {
        $this->fileManager = $fileManager;
    }
    
    public function setFileManager($fileManager)
    {
        $this->fileManager = $fileManager;
    }    
    /*
        Returns the gallery created. Needs the css file "file_explorer.css" (found on root/css).
        Parameters:
         $type = smallIcon, mediumIcon, largeIcon, details.
         $opts:
            $opts['url'] = url where the file will open or download. Defaults to root/common/download_file.php.
    */
    public function getHTML($type = "mediumIcon", $opts=array(),$downloadable = true, $edit = false)
    {
        if(!count($this->fileManager->getID()))
        {
            $this->_error("No se encontr&oacute; la galer&iacute;a de archivos.");
            return;
        }
        if(!count($this->fileManager->_arFMItems))
        {
            $str = "No hay archivos adjuntos.";
        }
        else
        {
            if($downloadable)
            {
                $url = $opts['url'] ? $opts['url'] : $GLOBALS["CONF"]["url"]["root"]."/common/download_file.php";    
                $url .= strstr($url, "?") ? "" : "?"; // put the ? at the end of the url only if there wasn't any.
                $url .= "file_managerID=".$this->fileManager->getID()."&fileID=";
            }
            $str .= "<div class='gallery'>";
            if($type == "details")
            {
                    $str .= "<div class='detailsTitle'>";
                    $str .= $edit ? "<div style='width:15px; float:left'>&nbsp;</div>" : "";
                    $str .= "<div class='fileName' >Nombre</div>";
                    $str .= "<div class='dateModified'>Fecha</div>";
                    $str .= "<div class='type'>Tipo</div>";
                    $str .= "</div>";
            }
            
            foreach($this->fileManager->_arFMItems as $item)
            {
                $i++;
                $fileOpts['attr'] = "onClick=\"document.location = '".($url.$item->getID())."'\"";
                $fileOpts['parity'] = $i;
                $str .= $item->getInfoThumb($type, $fileOpts, $downloadable, $edit);
            }
            $str .="<br clear='all'></div>";
        }
        $str .= $edit ? "<br><div><em>Clickee en el checkbox al lado del nombre del archivo para borrar.</em></div>" : "";
        return $str;
    }
    
    public function getXML()
    {
        $str = '<?xml version="1.0" encoding="utf-8"?>
                <files id="'.$this->fileManager->name.'" title="'.$this->fileManager->name.'">
                <description><[CDATA['.$this->fileManager->description.']]></description>
                    <items>
                    ';         
        foreach($this->fileManager->_arFMItems as $item)
        {
            $str .='<item id="'.$item->getID().'" url="'.$item->getUrl().'" mime="'.$item->type.'" extension="'.$item->getExtension().'">
                    <title><[CDATA['.$item->getFileTitle().']]></title>
                    <description><[CDATA['.$item->description.']]></description>
                    </item>
                    ';
        }
        $str .=    '</items>
                </files>';
        return $str;
    }
    
    public function printXML()
    {
        echo $this->getXML();
    }
    
    public function printHTML($type = "mediumIcon", $opts=array(),$downloadable = true, $edit = false)
    {
        echo $this->getHTML($type, $opts, $downloadable,$edit);
    }

    public function getUploadBox($maxAttachments = 0,$lastButtons = "")
    {
            $maxAttachments = $maxAttachments ? $maxAttachments : $this->maxAttachments;
            $str .=  '  <table class="ListingTable" style="margin-bottom:0px" id="'.$this->uploadPrefix.'files_list">
                        <tr class="TableTitles">
                        <th>Archivo</th>
                        <th class="LastColumn">&nbsp;</th>
                        </tr>
                        </table>
                        <table class="ListingTable">
                        <tr class="rowButtons">
                        <td colspan="2">
                        <input type="file" name="attachment" id="'.$this->uploadPrefix.'attachment">';
            $str .= $lastButtons ? '<div align="right">'.$lastButtons.'</div>' : "";                
            $str .=     '</td>
                        </tr>
                        </table>';
            if($this->uploadPrefix)
            {
                $uploadPrefix = "multi_selector.addPrefix( '".$this->uploadPrefix."' );";
            }
            $str .= '<script>
                        var multi_selector = new MultiSelector( \''.$this->uploadPrefix.'files_list\', '.$maxAttachments.');
                        '.$uploadPrefix.'
                        multi_selector.addElement( $( \''.$this->uploadPrefix.'attachment\' ) );
                        
                        multi_selector.setDeleteIcon("'.$GLOBALS["CONF"]["url"]["admin"].'/images/content/ico_trashcan.gif");
                    </script>';
            return $str;    
    }
    public function printUploadBox($maxAttachments = 0,$lastButtons = "")
    {
        echo $this->getUploadBox($maxAttachments,$lastButtons);
    }
}
?>
